function ajaxErrorHandler(err) {
	alert(err, null);
}
dwr.engine.setErrorHandler(ajaxErrorHandler);

function onHome() {
	window.location.href="index.jsp";
}
function onShowMember() {
	var card = Card.readSerial();
	if (card == null) {
		$alert("请插入您的会员卡后继续", null);
		return;
	}
	window.location.href="showMember.jsp?card=" + card;
}
function onLiving() {
	var card = Card.readSerial();
	if (card == null){
		$alert("请插入您的会员卡后继续");
		return;
	}
	CardAjax.selfServiceLiving(card, function(data){
		if (data == null){
			$alert("无法使用自助服务, 请到前台办理相关手续", null);
			return;
		}
		if (data.result == 1) {
			window.location.href="living.jsp?card=" + card;
		} else {
			$alert(data.description);
		}
	});
}
function onBackLiving() {
  var card =Card.readSerial();
	CardAjax.selfServiceCheckOut(card, function(data){
		if (data == null){
			$alert("无法使用自助服务, 请到前台办理相关手续", null);
			return;
		}
		if (data.result == 1) {
			  window.location.href="checkOutIndex.jsp?card="+card;
		} else {
			$alert(data.description);
		}
	});
}
function onQuery() {
	window.location.href="query.jsp";
}
function onBack() {
	history.go(-1);
}
function onHelp(){
	window.location.href="help.jsp";
}
function on0(){
	onHome();
}
function on1(){
	onLiving();
}
function on2(){
	onShowMember();
}
function on3(){
	onBack();
}
function on4(){
	onHelp();
}
$(function(){
	$("#container").append($("#selfhelpContent"));
	$("#selfhelpContent").height($("#container").height());
	$("#imageArea").width($(document).width()-500);
	var index = 0;
	var width = ($("#toolbar").width() - 20)/5-16;
//	alert(width);
	$("#toolbar > a").each(function(i, link){
		if (window.toolIndex == index){
			$(link).removeClass("blue").addClass("green");
		}
		$(link).attr("toolId", index);
//		alert($(link).attr("toolId"));
		index ++;
		$(link).width(width + "px");
		$(link).click(function(){
			var index = $(this).attr("toolId");
			if (index !== window.toolIndex){
				$.globalEval("on" + index + "()");
			}
		});
	});
});

function __Timer__ (label, func){
	this.label = label;
	this.func = func;
	this.lastTime = new Date().getTime();
	this.expire = 20;
	this.showTip = true;
	this.start = function(){
		this.stop();
		var THIS = this;
		$(this.label).everyTime('3s', function(){run(THIS);});
		var fn = function(){onEvent(THIS);};
		this.eventFunction = fn;
		$(document).mousemove(fn);
		$(document).keypress(fn);
		$(document).click(fn);
//		$("iframe").each(function(i, e){
//			var d = e.contentDocument || e.document;
//			$(d).mousemove(fn);
//			$(d).keypress(fn);
//			$(d).click(fn);
//		});
		show("start");
	};
	this.stop = function(){
		$(this.label).stopTime();
		var fn = this.eventFunction;
		if (typeof fn == 'function'){
			$(document).unbind('mousemove', fn);
			$(document).unbind('keypress', fn);
			$(document).unbind('click', fn);
//			$("iframe").each(function(i, e){
//				var d = e.contentDocument || e.document;
//				$(d).unbind('mousemove', fn);
//				$(d).unbind('keypress', fn);
//				$(d).unbind('click', fn);
//			});
			this.eventFunction = null;
		}
		show("stop");
	};
	
	this.onFrameEvent = function(){
		this.lastTime = new Date().getTime();
		$(this.label).hide();
		show("onFrameEvent");
	};
	
	function onEvent(timer){
		timer.lastTime = new Date().getTime();
		$(timer.label).hide();
		show("onEvent");
	}
	
	function run(timer) {
		var now = new Date().getTime();
		var d = Math.ceil((now - timer.lastTime)/1000);
		var o = $(timer.label);
		if (d > timer.expire){
			if (timer.func(timer, d)) {
				timer.lastTime = now;
			}
			o.hide();
			show("call");
		} else if (timer.showTip && d > timer.expire / 2) {
			var s = Math.ceil(timer.expire - d);
			o.text("长时间没有任何操作，即将在 " + s + " 秒内返回首页");
			o.show(1000);
			show(s + "s");
		} else {
			show("run " + (now - timer.lastTime));
		}
	}
	function show(text) {
		//$("div#test").html(text);
	}
}

function onFrameEvent(){
	if (window.Timer && window.Timer.onFrameEvent)
		Timer.onFrameEvent();
}

function showTips(tips) {
	var o = $("#timeoutTip");
	o.html(tips);
	o.show();
}

function onTimeout() {
	window.location.href = Context.Path + "/modules/selfhelp/";
	return true;
}
function showTimeoutTip() {
	return true;
}
$(function(){
	var Timer = window.Timer = new __Timer__("#timeoutTip", function(){
		return onTimeout();
	});
	if (!showTimeoutTip())
		Timer.showTip = false;
	Timer.start();
	
	$('body').everyTime('3m', function(){
		CardAjax.noop(function(){});
	});
});
